package com.appbee.cm.client.modular.ui;

import javax.inject.Inject;

import com.appbee.cm.client.modular.presenter.ModulSelectionBoxPresenter;
import com.appbee.cm.client.nav.ui.INavSelectStyle;
import com.appbee.core.client.modular.IModul;
import com.google.gwt.core.client.GWT;
import com.google.gwt.dom.client.Element;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HasText;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Widget;

public class ModulSelectionBox extends Composite implements
		ModulSelectionBoxPresenter.View, HasText
{

	private static ModulSelectionBoxUiBinder uiBinder = GWT
			.create(ModulSelectionBoxUiBinder.class);

	interface ModulSelectionBoxUiBinder extends
			UiBinder<Widget, ModulSelectionBox>
	{
	}

	@UiField
	Element label;

	@UiField
	Label value;

	@UiField
	Button selectButton;

	private final ModulSelectionBoxPresenter presenter;

	@UiField(provided = true)
	INavSelectStyle style;

	@Inject
	public ModulSelectionBox(	ModulSelectionBoxPresenter modulPresenter,
								EventBus eventBus,
								INavSelectStyle navSelectStyle)
	{
		this.presenter = modulPresenter;
		this.style = navSelectStyle;
		presenter.setView(this);
		initWidget(uiBinder.createAndBindUi(this));
		selectButton.addClickHandler(new ClickHandler()
		{

			@Override
			public void onClick(ClickEvent event)
			{
				presenter.onSelectClick(value);
			}
		});
	}

	@Override
	public void setValue(IModul modul)
	{
		value.setText(modul == null ? "" : modul.getName());
	}

	public IModul getValue()
	{
		return presenter.getValue();
	}

	public void setModulId(String modulId)
	{
		presenter.setModulIdValue(modulId);
	}

	@Override
	public String getText()
	{
		return label.getInnerText();
	}

	@Override
	public void setText(String text)
	{
		label.setInnerText(text);
	}

	public ModulSelectionBoxPresenter getPresenter()
	{
		return presenter;
	}

}
